package com.blb.ssh.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;

import com.blb.ssh.dao.MenuDao;
import com.blb.ssh.entity.Menu;

public class MenuDaoImpl implements MenuDao {
	
	private SessionFactory sessionFactory;
	
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	

	
	@Override
	public List<Menu> getMenuDataByRoleId(int roleId) {
		Session session =sessionFactory.getCurrentSession();
		String sql = "select t2.* from t_role_menu t1 , t_menu t2 where t1.menu_id = t2.menu_id and role_id=:role_id ";
		List<Menu> list = new ArrayList<Menu>();
		@SuppressWarnings("unchecked")
		NativeQuery<Menu> nativeQuery = session.createSQLQuery(sql);
		nativeQuery.setParameter("role_id", roleId);
		for(Menu menu:nativeQuery.addEntity(Menu.class).list())
		{
			Menu m = new Menu();
			m.setMenuId(menu.getMenuId());
			m.setMenuName(menu.getMenuName());
			m.setMenuPid(menu.getMenuPid());
			m.setMenuUrl(menu.getMenuUrl());
			m.setMenuRole(menu.getMenuRole());
			m.setMenuType(menu.getMenuType());
			list.add(m);
			
		}
	
		return list;
		
	}
	
	
	
	@Override
	public List<Menu> getMenuDataByPid(int i) {
		Session session =sessionFactory.getCurrentSession();
		@SuppressWarnings("unchecked")
		Query<Menu> query = session.createQuery("from Menu where menuPid=:i");
		query.setParameter("i", i);
		List<Menu> list = query.list();
		return list;
	}

	
	@Override
	public List<Menu> getMenuAll(int page,int rows){
			Session session =sessionFactory.getCurrentSession();
			String sql = "select t2.* from t_role_menu t1 , t_menu t2 where t1.menu_id = t2.menu_id group by menu_id";
			List<Menu> list = new ArrayList<Menu>();
			@SuppressWarnings("unchecked")
			NativeQuery<Menu> nativeQuery = session.createSQLQuery(sql);
			nativeQuery.setFirstResult((page-1)*rows);
			nativeQuery.setMaxResults(rows);
			for(Menu menu:nativeQuery.addEntity(Menu.class).list())
			{
				Menu m = new Menu();
				m.setMenuId(menu.getMenuId());
				m.setMenuName(menu.getMenuName());
				m.setMenuPid(menu.getMenuPid());
				m.setMenuUrl(menu.getMenuUrl());
				m.setMenuRole(menu.getMenuRole());
				m.setMenuType(menu.getMenuType());
				list.add(m);
				
			}
		
			return list;
	}
	
	@Override
	public boolean addMenu(Menu menu) {
		Session session = sessionFactory.getCurrentSession();
		boolean isok = false;
		Menu m = new Menu();
		m.setMenuId(menu.getMenuId());
		m.setMenuName(menu.getMenuName());
		m.setMenuPid(menu.getMenuPid());
		m.setMenuRole(menu.getMenuRole());
		m.setMenuType(menu.getMenuType());
		m.setMenuUrl(menu.getMenuUrl());
		if(sellectMenuName(menu.getMenuName()))
		{
			return isok;
		}
		else
		{
			session.save(m);
			isok = true;
			return isok;
		}
		
	}
	
	/**
	 * 查重
	 * @param menuName
	 * @return
	 */
	private boolean sellectMenuName(String menuName) {
		Session session = sessionFactory.getCurrentSession();

		boolean isok = false;
		@SuppressWarnings("unchecked")
		Query<Menu> query = session.createQuery("from Menu");
		
		for(Menu menu:query.list())
		{
			if(menuName.equals(menu.getMenuName()))
			{
				isok = true;
				break;
			}
		}
		return isok;
	}

	@Override
	public boolean batchDeleteMenu(String menuIds) {
		Session session = sessionFactory.getCurrentSession();
		
		boolean isok = true;
		
		
		String[]  menuIdsstr=StringUtils.split(menuIds, ",");
		String hql="";
		for(int i=0;i<menuIdsstr.length;i++){
			if(i ==0){
				hql="id='"+menuIdsstr[i]+"'";
			}else{
				hql=hql+" or id='"+menuIdsstr[i]+"'";
			}
		}
		System.out.println(hql);
		Query<?> query = session.createQuery("delete from Menu where "+hql);
		query.executeUpdate();
		
		return isok;
	}

	@Override
	public Long getMenuCount() {
		Session session =sessionFactory.getCurrentSession();
		
		String hql="select count(*) from Menu";
		@SuppressWarnings("unchecked")
		Query<Long> query = session.createQuery(hql);
		@SuppressWarnings("deprecation")
		long count=query.iterate().next().intValue();
		
	
		return count;
	}

	@Override
	public boolean editMenu(Menu menu) {
		Session session =sessionFactory.getCurrentSession();
		
		
		boolean isok=true;
		Menu m = session.get(Menu.class,menu.getMenuId());
		m.setMenuId(menu.getMenuId());
		m.setMenuName(menu.getMenuName());
		m.setMenuPid(menu.getMenuPid());
		m.setMenuRole(menu.getMenuRole());
		m.setMenuType(menu.getMenuType());
		m.setMenuUrl(menu.getMenuUrl());
		
		session.update(m);
		
		return isok;
	}

	@Override
	public List<Menu> getMenuPidAll() {
		Session session = sessionFactory.getCurrentSession();

		String sql = "SELECT t2.* FROM (SELECT * FROM t_menu ) t1 ,t_menu t2 WHERE t1.menu_pid = t2.menu_id GROUP BY menu_id";
		List<Menu> list = new ArrayList<Menu>();
		@SuppressWarnings("unchecked")
		NativeQuery<Menu> nativeQuery = session.createSQLQuery(sql);
		for(Menu menu:nativeQuery.addEntity(Menu.class).list())
		{
			Menu m = new Menu();
			m.setMenuId(menu.getMenuId());
			m.setMenuName(menu.getMenuName());
			m.setMenuPid(menu.getMenuPid());
			m.setMenuUrl(menu.getMenuUrl());
			m.setMenuRole(menu.getMenuRole());
			m.setMenuType(menu.getMenuType());
			list.add(m);
		}
		System.out.println(list.toString());
		return list;
	}

	@Override
	public List<Menu> selectAllmenuTypeList() {
		Session session =sessionFactory.getCurrentSession();
		String sql = "select * from t_menu  group by menu_type";
		List<Menu> list = new ArrayList<Menu>();
		@SuppressWarnings("unchecked")
		NativeQuery<Menu> nativeQuery = session.createSQLQuery(sql);
		for(Menu menu:nativeQuery.addEntity(Menu.class).list())
		{
			Menu m = new Menu();
			m.setMenuId(menu.getMenuId());
			m.setMenuName(menu.getMenuName());
			m.setMenuPid(menu.getMenuPid());
			m.setMenuUrl(menu.getMenuUrl());
			m.setMenuRole(menu.getMenuRole());
			m.setMenuType(menu.getMenuType());
			list.add(m);
			
		}
		System.out.println(list.toString());
		return list;
	}







	



}
